package biz.ezcom.spring.datasource.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.transaction.annotation.Transactional;

import biz.ezcom.spring.datasource.bean.Person;
import biz.ezcom.spring.datasource.dao.PersonDao;

@Transactional
public class PersonDaoImpl implements PersonDao {
	private JdbcTemplate jdbcTemplate;
	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	@Override
	public void save(Person person) {
		String sql = "insert into person(id,username,password,age,registerDate) values(?,?,?,?,?)";
		Object[] args = new Object[]{person.getId(),person.getUsername(), person.getPassword(), person.getAge(), person.getRegisterDate()};
		int[] argTypes = new int[]{Types.INTEGER, Types.VARCHAR,Types.VARCHAR,Types.INTEGER,Types.DATE};
		jdbcTemplate.update(sql, args, argTypes);
	}

	@Override
	public void update(Person person) {
		String sql = "update person set username=?,password=?,age=?,registerDate=? where id=?";
		Object[] args = new Object[]{person.getUsername(), person.getPassword(), person.getAge(), person.getRegisterDate(),person.getId()};
		int[] argTypes = new int[]{Types.VARCHAR,Types.VARCHAR,Types.INTEGER,Types.DATE,Types.INTEGER};
		jdbcTemplate.update(sql, args, argTypes);
	}

	@Override
	public Person getPerson(int id) {
		String sql = "select id,username,password,age,registerDate from person where id=?";
		Object[] args = new Object[]{id};
		Person person = (Person) jdbcTemplate.queryForObject(sql, args, new RowMapper(){
			@Override
			public Object mapRow(ResultSet rs, int index) throws SQLException {
				Person person = new Person();
				person.setId(rs.getInt("id"));
				person.setUsername(rs.getString("username"));
				person.setPassword(rs.getString("password"));
				person.setAge(rs.getInt("age"));
				person.setRegisterDate(rs.getDate("registerDate"));
				return person;
			}
		});
		return person;
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<Person> getPersons() {
		String sql = "select id,username,password,age,registerDate from person";
		List<Person> list = jdbcTemplate.query(sql, new RowMapper(){
			@Override
			public Object mapRow(ResultSet rs, int index) throws SQLException {
				Person person = new Person();
				person.setId(rs.getInt("id"));
				person.setUsername(rs.getString("username"));
				person.setPassword(rs.getString("password"));
				person.setAge(rs.getInt("age"));
				person.setRegisterDate(rs.getDate("registerDate"));
				return person;
			}
		});
		return list;
	}

	@Override
	public void delete(int id) {
		String sql = "delete from person where id=?";
		Object[] args = new Object[]{id};
		int[] argTypes = new int[]{Types.INTEGER};
		jdbcTemplate.update(sql, args, argTypes);
	}
	
	@Override
	public void truncate(String tableName) {
		String sql = "truncate table " + tableName+";";
		jdbcTemplate.execute(sql);
	}

}
